package org.rqb.ui.button;

import javax.swing.*;
import java.awt.*;

public class MyGlassButton extends JButton {
    public MyGlassButton(String text) {
        super(text);

        // 🌈 基础样式
        this.setOpaque(false);
        this.setContentAreaFilled(true);
        this.setFocusPainted(false);
        this.setForeground(Color.WHITE);
        this.setFont(new Font("Microsoft YaHei", Font.BOLD, 18));
        this.setBorder(BorderFactory.createLineBorder(new Color(255, 255, 255, 100), 2));
        this.setBackground(new Color(0, 0, 0, 100)); // 半透明黑底，突显按钮

        // 🌟 圆角 + 自定义绘制（使按钮半透明更自然）
        this.setUI(new javax.swing.plaf.basic.BasicButtonUI() {
            @Override
            public void paint(Graphics g, JComponent c) {
                Graphics2D g2 = (Graphics2D) g.create();
                JButton b = (JButton) c;
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                // 背景圆角矩形
                if (b.getModel().isRollover()) {
                    g2.setColor(new Color(255, 255, 255, 60)); // hover 时背景略亮
                } else {
                    g2.setColor(b.getBackground());
                }
                g2.fillRoundRect(0, 0, b.getWidth(), b.getHeight(), 20, 20);

                // 边框（hover 时发光）
                if (b.getModel().isRollover()) {
                    g2.setColor(new Color(255, 255, 255, 180));
                    g2.setStroke(new BasicStroke(2f));
                    g2.drawRoundRect(0, 0, b.getWidth() - 1, b.getHeight() - 1, 20, 20);
                }

                // 绘制文字
                FontMetrics fm = g2.getFontMetrics();
                Rectangle r = new Rectangle(0, 0, b.getWidth(), b.getHeight());
                int textX = (r.width - fm.stringWidth(b.getText())) / 2;
                int textY = (r.height - fm.getHeight()) / 2 + fm.getAscent();
                g2.setColor(b.getForeground());
                g2.drawString(b.getText(), textX, textY);

                g2.dispose();
            }
        });

        // 🌠 鼠标进入/离开时动态变化
        this.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseEntered(java.awt.event.MouseEvent e) {
                MyGlassButton.this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                MyGlassButton.this.setBackground(new Color(255, 255, 255, 60));
            }

            @Override
            public void mouseExited(java.awt.event.MouseEvent e) {
                MyGlassButton.this.setBackground(new Color(0, 0, 0, 100));
            }
        });
    }
}
